//
//  CCQSportSportingViewController.m
//  sunduskXingyun
//
//  Created by 神威 on 16/10/21.
//  Copyright © 2016年 ccq. All rights reserved.
//
///测试
#import "CCQSportSportingViewController.h"
#import "CCQSportMapViewController.h"
#import "CCQSportCameraViewController.h"
#import "CCQSportSpeaker.h"
#import "CZAdditions.h"
@interface CCQSportSportingViewController ()<CCQSportMapViewControllerDelegate>

@property (weak, nonatomic) IBOutlet UIButton *mapButton;

/**
 运动地图控制器
 */
@property (strong , nonatomic) CCQSportMapViewController *mapViewController;

/**
 距离标签
 */
@property (weak, nonatomic) IBOutlet UILabel *distanceLabel;

/**
 时间标签
 */
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

/**
 速度标签
 */
@property (weak, nonatomic) IBOutlet UILabel *speedLabel;
@property (weak, nonatomic) IBOutlet UIButton *pauseButton;

@property (weak, nonatomic) IBOutlet UIButton *continueButton;

/**
 暂停按钮的中心X约束
 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pauseButtonCenterXCons;

/**
 继续按钮的中心X约束
 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *continueButtonCenterXCons;

/**
 结束按钮的中心X约束
 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *finishButtonCenterXCons;

@end

@implementation CCQSportSportingViewController{
    // 语音播报器
    CCQSportSpeaker *_sportSpeaker;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _sportSpeaker = [CCQSportSpeaker new];
    
    [self setupMapViewController];
    [self setupBackgroundLayer];
}

// 所有子控件布局完成，适合进一步设置其他控件的位置
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    // 设置罗盘位置
    CGFloat x = _mapButton.center.x - _mapViewController.mapView.compassSize.width * 0.5;
    CGFloat y = _mapButton.center.y - _mapViewController.mapView.compassSize.height * 0.5;
    
    _mapViewController.mapView.compassOrigin = CGPointMake(x, y);
    
}
#pragma mark - 监听方法
- (IBAction)showCameraViewController {
    
    CCQSportCameraViewController *vc = [[CCQSportCameraViewController alloc] init];
    [self presentViewController:vc animated:YES completion:nil];
}

- (IBAction)showMapViewController {
    
   
    
    // 3. 模拟展现
    [self presentViewController:_mapViewController animated:YES completion:nil];
    
 
}
/**
 修改运动状态

 @param button button
 */
- (IBAction)changeSportState:(UIButton *)button{
    // 从按钮的 tag 获取运动状态
    CCQSportState state = button.tag;
    
    //修改地图控制器的运动状态
    _mapViewController.sportTracking.sportState = state;
    
    // 1. 根据按钮的 tag 来决定按钮的偏移量
    CGFloat offset = (state == CCQSportStatePause) ? -80 : 80;
    // 2. 设置按钮的约束值
    _pauseButtonCenterXCons.constant += offset;
    _continueButtonCenterXCons.constant += offset;
    _finishButtonCenterXCons.constant -= offset;
    
    [UIView animateWithDuration:0.25 animations:^{
        
        // 3. 隐藏暂停按钮
        _pauseButton.alpha = (button != _pauseButton);
        
        [self.view layoutIfNeeded];
    }];
    
    // 播放语音
    [_sportSpeaker sportStateChanged:state];
    
}
#pragma mark - CCQSportMapViewControllerDelegate
- (void)sportMapViewControllerDidChangedData:(CCQSportMapViewController *)vc{
    // 获取地图控制器的轨迹追踪模型
    CCQSportTracking *model = vc.sportTracking;
    
    //更新UI
    _distanceLabel.text = [NSString stringWithFormat:@"%0.2f", model.totalDistance];
    _timeLabel.text = model.totalTimeStr;
    _speedLabel.text = [NSString stringWithFormat:@"%0.2f", model.avgSpeed];
    
    // 根据运动状态的辩护模拟状态按钮点击操作
    
    // 1. 当用户暂停运动，同时暂停按钮可见，模拟点击暂停按钮，隐藏暂停按钮
    if (model.sportState == CCQSportStatePause && _pauseButton.alpha == 1) {
        // 点击暂停按钮
        [self changeSportState:_pauseButton];
    }
    
    // 2. 当用户继续运行，同时暂停按钮不可见，模拟点击继续按钮，显示暂停按钮
    if (model.sportState == CCQportStateContinue && _pauseButton.alpha == 0) {
        [self changeSportState:_continueButton];
    }
    // 3. 播放距离提示音
    [_sportSpeaker reportWithDistance:model.totalDistance time:model.totalTime speed:model.avgSpeed];

}


#pragma mark - 设置界面

/**
 设置背景图层
 */
-(void)setupBackgroundLayer{
    
    // 线性 渐变图层
    CAGradientLayer *layer = [CAGradientLayer layer];
    
    //指定位置
    layer.bounds = self.view.bounds;
    layer.position = self.view.center;
    
    // 设置渐变色数组
    CGColorRef color1 = [UIColor cz_colorWithHex:0x0e1428].CGColor;
    CGColorRef color2 = [UIColor cz_colorWithHex:0x406479].CGColor;
    CGColorRef color3 = [UIColor cz_colorWithHex:0x406578].CGColor;
    
    // 桥接
    layer.colors = @[(__bridge UIColor *)color1, (__bridge UIColor *)color2, (__bridge UIColor *)color3];
    
    // 设置颜色的位置数据
    layer.locations = @[@0, @0.6, @1];
    
    // 将图层插入到最底层
    [self.view.layer insertSublayer:layer atIndex:0];
}

/// 设置地图视图控制器

- (void)setupMapViewController{

    // 1. 通过 storyboard 实例化地图控制器
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"CCQSportSporting" bundle:nil];
    
    _mapViewController = [sb instantiateViewControllerWithIdentifier:@"sportMapViewController"];
    
    // 2. 设置运动轨迹模型
    _mapViewController.sportTracking = [[CCQSportTracking alloc] initWithType:_sportType state:CCQportStateContinue];
    
    // 3. 设置代理
    _mapViewController.delegate = self;
    
    // 4. 开始运动的语音播报
    [_sportSpeaker startSportWithType:_sportType];
}

@end
